package com.yin.jia.liang.controller;

import com.yin.jia.liang.entity.Goods;
import com.yin.jia.liang.entity.GoodsOrder;
import com.yin.jia.liang.entity.User;
import com.yin.jia.liang.service.GoodsService;
import com.yin.jia.liang.service.OrderService;
import com.yin.jia.liang.vo.GoodsOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        List<GoodsOrder> orders = null;
        if ("admin".equals(user.getUsername())) {
            orders = orderService.find();
        } else {
            orders = orderService.findByUserId(user.getId());
        }

        List<GoodsOrderVo> goodsOrderVos = new ArrayList<>();

        for (GoodsOrder order: orders) {
            GoodsOrderVo o = new GoodsOrderVo();

            Goods goods = goodsService.findById(order.getGoodsId());

            o.setGoods(goods);
            o.setUserId(order.getUserId());
            o.setAddress(order.getAddress());
            o.setPhone(order.getPhone());
            o.setName(order.getName());
            o.setMoney(order.getMoney());
            o.setId(order.getId());
            o.setGoodsId(order.getGoodsId());
            o.setCount(order.getCount());

            goodsOrderVos.add(o);
        }

        model.addAttribute("orders", goodsOrderVos);
        return "order/list";
    }


    @RequestMapping(value = "add/{goodsId}", method = RequestMethod.GET)
    public String index(HttpSession session, @PathVariable Integer goodsId, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        Goods goods = goodsService.findById(goodsId);
        model.addAttribute("goods", goods);
        return "order/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String index(@ModelAttribute GoodsOrder goodsOrder, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        goodsOrder.setUserId(user.getId());
        orderService.save(goodsOrder);
        return "redirect:/order/list";
    }

    @RequestMapping(value = "del/{id}", method = RequestMethod.GET)
    public String del(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        if ("admin".equals(user.getUsername())) {
            orderService.delete(id);
        }
        return "redirect:/order/list";
    }
}
